package keshang;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
public class DomXml {
    private Document document=null;
    public void getDocument(){
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        try{
            DocumentBuilder builder=factory.newDocumentBuilder();
            document=builder.parse("手机信息.tld");
        }catch (ParserConfigurationException e){
            e.printStackTrace();
        }catch (SAXException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public  void selectNodes(){
        NodeList nodeList=document.getElementsByTagName("Brand");
        for(int i=0;i< nodeList.getLength();i++){
            Node node=nodeList.item(i);
            Element em=(Element)node;
            String name=em.getAttribute("name");
            NodeList childNodes=em.getChildNodes();
            for(int j=0;j<childNodes.getLength();j++){
                Node node1=childNodes.item(j);
                if(node1.getNodeType()==Node.ELEMENT_NODE){
                    Element em1=(Element)node1;
                    String name1=em1.getAttribute("name");
                    System.out.println(name1);
                }
            }
        }
    }
    public static void main(String[] args){
        DomXml domXml=new DomXml();
        domXml.getDocument();
        domXml.selectNodes();
    }
}

